// Copyright (c) Lawrence Livermore National Security, LLC and other VisIt
// Project developers.  See the top-level LICENSE file for dates and other
// details.  No copyright assignment is required to contribute to VisIt.

#include <PyExtractPointFunction2DAttributes.h>
#include <ObserverToCallback.h>
#include <stdio.h>
#include <Py2and3Support.h>

// ****************************************************************************
// Module: PyExtractPointFunction2DAttributes
//
// Purpose:
//   Attributes for ExtractPointFunction2D
//
// Note:       Autogenerated by xml2python. Do not modify by hand!
//
// Programmer: xml2python
// Creation:   omitted
//
// ****************************************************************************

//
// This struct contains the Python type information and a ExtractPointFunction2DAttributes.
//
struct ExtractPointFunction2DAttributesObject
{
    PyObject_HEAD
    ExtractPointFunction2DAttributes *data;
    bool        owns;
    PyObject   *parent;
};

//
// Internal prototypes
//
static PyObject *NewExtractPointFunction2DAttributes(int);
std::string
PyExtractPointFunction2DAttributes_ToString(const ExtractPointFunction2DAttributes *atts, const char *prefix, const bool forLogging)
{
    std::string str;
    char tmpStr[1000];

    {   const intVector &I = atts->GetI();
        snprintf(tmpStr, 1000, "%sI = (", prefix);
        str += tmpStr;
        for(size_t i = 0; i < I.size(); ++i)
        {
            snprintf(tmpStr, 1000, "%d", I[i]);
            str += tmpStr;
            if(i < I.size() - 1)
            {
                snprintf(tmpStr, 1000, ", ");
                str += tmpStr;
            }
        }
        snprintf(tmpStr, 1000, ")\n");
        str += tmpStr;
    }
    {   const intVector &J = atts->GetJ();
        snprintf(tmpStr, 1000, "%sJ = (", prefix);
        str += tmpStr;
        for(size_t i = 0; i < J.size(); ++i)
        {
            snprintf(tmpStr, 1000, "%d", J[i]);
            str += tmpStr;
            if(i < J.size() - 1)
            {
                snprintf(tmpStr, 1000, ", ");
                str += tmpStr;
            }
        }
        snprintf(tmpStr, 1000, ")\n");
        str += tmpStr;
    }
    return str;
}

static PyObject *
ExtractPointFunction2DAttributes_Notify(PyObject *self, PyObject *args)
{
    ExtractPointFunction2DAttributesObject *obj = (ExtractPointFunction2DAttributesObject *)self;
    obj->data->Notify();
    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
ExtractPointFunction2DAttributes_SetI(PyObject *self, PyObject *args)
{
    ExtractPointFunction2DAttributesObject *obj = (ExtractPointFunction2DAttributesObject *)self;

    intVector vec;

    if (PyNumber_Check(args))
    {
        long val = PyLong_AsLong(args);
        int cval = int(val);
        if (val == -1 && PyErr_Occurred())
        {
            PyErr_Clear();
            return PyErr_Format(PyExc_TypeError, "number not interpretable as C++ int");
        }
        if (fabs(double(val))>1.5E-7 && fabs((double(long(cval))-double(val))/double(val))>1.5E-7)
            return PyErr_Format(PyExc_ValueError, "number not interpretable as C++ int");
        vec.resize(1);
        vec[0] = cval;
    }
    else if (PySequence_Check(args) && !PyUnicode_Check(args))
    {
        vec.resize(PySequence_Size(args));
        for (Py_ssize_t i = 0; i < PySequence_Size(args); i++)
        {
            PyObject *item = PySequence_GetItem(args, i);

            if (!PyNumber_Check(item))
            {
                Py_DECREF(item);
                return PyErr_Format(PyExc_TypeError, "arg %d is not a number type", (int) i);
            }

            long val = PyLong_AsLong(item);
            int cval = int(val);

            if (val == -1 && PyErr_Occurred())
            {
                Py_DECREF(item);
                PyErr_Clear();
                return PyErr_Format(PyExc_TypeError, "arg %d not interpretable as C++ int", (int) i);
            }
            if (fabs(double(val))>1.5E-7 && fabs((double(long(cval))-double(val))/double(val))>1.5E-7)
            {
                Py_DECREF(item);
                return PyErr_Format(PyExc_ValueError, "arg %d not interpretable as C++ int", (int) i);
            }
            Py_DECREF(item);

            vec[i] = cval;
        }
    }
    else
        return PyErr_Format(PyExc_TypeError, "arg(s) must be one or more ints");

    obj->data->GetI() = vec;
    // Mark the I in the object as modified.
    obj->data->SelectI();

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
ExtractPointFunction2DAttributes_GetI(PyObject *self, PyObject *args)
{
    ExtractPointFunction2DAttributesObject *obj = (ExtractPointFunction2DAttributesObject *)self;
    // Allocate a tuple the with enough entries to hold the I.
    const intVector &I = obj->data->GetI();
    PyObject *retval = PyTuple_New(I.size());
    for(size_t i = 0; i < I.size(); ++i)
        PyTuple_SET_ITEM(retval, i, PyInt_FromLong(long(I[i])));
    return retval;
}

/*static*/ PyObject *
ExtractPointFunction2DAttributes_SetJ(PyObject *self, PyObject *args)
{
    ExtractPointFunction2DAttributesObject *obj = (ExtractPointFunction2DAttributesObject *)self;

    intVector vec;

    if (PyNumber_Check(args))
    {
        long val = PyLong_AsLong(args);
        int cval = int(val);
        if (val == -1 && PyErr_Occurred())
        {
            PyErr_Clear();
            return PyErr_Format(PyExc_TypeError, "number not interpretable as C++ int");
        }
        if (fabs(double(val))>1.5E-7 && fabs((double(long(cval))-double(val))/double(val))>1.5E-7)
            return PyErr_Format(PyExc_ValueError, "number not interpretable as C++ int");
        vec.resize(1);
        vec[0] = cval;
    }
    else if (PySequence_Check(args) && !PyUnicode_Check(args))
    {
        vec.resize(PySequence_Size(args));
        for (Py_ssize_t i = 0; i < PySequence_Size(args); i++)
        {
            PyObject *item = PySequence_GetItem(args, i);

            if (!PyNumber_Check(item))
            {
                Py_DECREF(item);
                return PyErr_Format(PyExc_TypeError, "arg %d is not a number type", (int) i);
            }

            long val = PyLong_AsLong(item);
            int cval = int(val);

            if (val == -1 && PyErr_Occurred())
            {
                Py_DECREF(item);
                PyErr_Clear();
                return PyErr_Format(PyExc_TypeError, "arg %d not interpretable as C++ int", (int) i);
            }
            if (fabs(double(val))>1.5E-7 && fabs((double(long(cval))-double(val))/double(val))>1.5E-7)
            {
                Py_DECREF(item);
                return PyErr_Format(PyExc_ValueError, "arg %d not interpretable as C++ int", (int) i);
            }
            Py_DECREF(item);

            vec[i] = cval;
        }
    }
    else
        return PyErr_Format(PyExc_TypeError, "arg(s) must be one or more ints");

    obj->data->GetJ() = vec;
    // Mark the J in the object as modified.
    obj->data->SelectJ();

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
ExtractPointFunction2DAttributes_GetJ(PyObject *self, PyObject *args)
{
    ExtractPointFunction2DAttributesObject *obj = (ExtractPointFunction2DAttributesObject *)self;
    // Allocate a tuple the with enough entries to hold the J.
    const intVector &J = obj->data->GetJ();
    PyObject *retval = PyTuple_New(J.size());
    for(size_t i = 0; i < J.size(); ++i)
        PyTuple_SET_ITEM(retval, i, PyInt_FromLong(long(J[i])));
    return retval;
}



PyMethodDef PyExtractPointFunction2DAttributes_methods[EXTRACTPOINTFUNCTION2DATTRIBUTES_NMETH] = {
    {"Notify", ExtractPointFunction2DAttributes_Notify, METH_VARARGS},
    {"SetI", ExtractPointFunction2DAttributes_SetI, METH_VARARGS},
    {"GetI", ExtractPointFunction2DAttributes_GetI, METH_VARARGS},
    {"SetJ", ExtractPointFunction2DAttributes_SetJ, METH_VARARGS},
    {"GetJ", ExtractPointFunction2DAttributes_GetJ, METH_VARARGS},
    {NULL, NULL}
};

//
// Type functions
//

static void
ExtractPointFunction2DAttributes_dealloc(PyObject *v)
{
   ExtractPointFunction2DAttributesObject *obj = (ExtractPointFunction2DAttributesObject *)v;
   if(obj->parent != 0)
       Py_DECREF(obj->parent);
   if(obj->owns)
       delete obj->data;
}

static PyObject *ExtractPointFunction2DAttributes_richcompare(PyObject *self, PyObject *other, int op);
PyObject *
PyExtractPointFunction2DAttributes_getattr(PyObject *self, char *name)
{
    if(strcmp(name, "I") == 0)
        return ExtractPointFunction2DAttributes_GetI(self, NULL);
    if(strcmp(name, "J") == 0)
        return ExtractPointFunction2DAttributes_GetJ(self, NULL);


    // Add a __dict__ answer so that dir() works
    if (!strcmp(name, "__dict__"))
    {
        PyObject *result = PyDict_New();
        for (int i = 0; PyExtractPointFunction2DAttributes_methods[i].ml_meth; i++)
            PyDict_SetItem(result,
                PyString_FromString(PyExtractPointFunction2DAttributes_methods[i].ml_name),
                PyString_FromString(PyExtractPointFunction2DAttributes_methods[i].ml_name));
        return result;
    }

    return Py_FindMethod(PyExtractPointFunction2DAttributes_methods, self, name);
}

int
PyExtractPointFunction2DAttributes_setattr(PyObject *self, char *name, PyObject *args)
{
    PyObject NULL_PY_OBJ;
    PyObject *obj = &NULL_PY_OBJ;

    if(strcmp(name, "I") == 0)
        obj = ExtractPointFunction2DAttributes_SetI(self, args);
    else if(strcmp(name, "J") == 0)
        obj = ExtractPointFunction2DAttributes_SetJ(self, args);

    if (obj != NULL && obj != &NULL_PY_OBJ)
        Py_DECREF(obj);

    if (obj == &NULL_PY_OBJ)
    {
        obj = NULL;
        PyErr_Format(PyExc_NameError, "name '%s' is not defined", name);
    }
    else if (obj == NULL && !PyErr_Occurred())
        PyErr_Format(PyExc_RuntimeError, "unknown problem with '%s'", name);

    return (obj != NULL) ? 0 : -1;
}

static int
ExtractPointFunction2DAttributes_print(PyObject *v, FILE *fp, int flags)
{
    ExtractPointFunction2DAttributesObject *obj = (ExtractPointFunction2DAttributesObject *)v;
    fprintf(fp, "%s", PyExtractPointFunction2DAttributes_ToString(obj->data, "",false).c_str());
    return 0;
}

PyObject *
ExtractPointFunction2DAttributes_str(PyObject *v)
{
    ExtractPointFunction2DAttributesObject *obj = (ExtractPointFunction2DAttributesObject *)v;
    return PyString_FromString(PyExtractPointFunction2DAttributes_ToString(obj->data,"", false).c_str());
}

//
// The doc string for the class.
//
#if PY_MAJOR_VERSION > 2 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 5)
static const char *ExtractPointFunction2DAttributes_Purpose = "Attributes for ExtractPointFunction2D";
#else
static char *ExtractPointFunction2DAttributes_Purpose = "Attributes for ExtractPointFunction2D";
#endif

//
// Python Type Struct Def Macro from Py2and3Support.h
//
//         VISIT_PY_TYPE_OBJ( VPY_TYPE,
//                            VPY_NAME,
//                            VPY_OBJECT,
//                            VPY_DEALLOC,
//                            VPY_PRINT,
//                            VPY_GETATTR,
//                            VPY_SETATTR,
//                            VPY_STR,
//                            VPY_PURPOSE,
//                            VPY_RICHCOMP,
//                            VPY_AS_NUMBER)

//
// The type description structure
//

VISIT_PY_TYPE_OBJ(ExtractPointFunction2DAttributesType,         \
                  "ExtractPointFunction2DAttributes",           \
                  ExtractPointFunction2DAttributesObject,       \
                  ExtractPointFunction2DAttributes_dealloc,     \
                  ExtractPointFunction2DAttributes_print,       \
                  PyExtractPointFunction2DAttributes_getattr,   \
                  PyExtractPointFunction2DAttributes_setattr,   \
                  ExtractPointFunction2DAttributes_str,         \
                  ExtractPointFunction2DAttributes_Purpose,     \
                  ExtractPointFunction2DAttributes_richcompare, \
                  0); /* as_number*/

//
// Helper function for comparing.
//
static PyObject *
ExtractPointFunction2DAttributes_richcompare(PyObject *self, PyObject *other, int op)
{
    // only compare against the same type 
    if ( Py_TYPE(self) != &ExtractPointFunction2DAttributesType
         || Py_TYPE(other) != &ExtractPointFunction2DAttributesType)
    {
        Py_INCREF(Py_NotImplemented);
        return Py_NotImplemented;
    }

    PyObject *res = NULL;
    ExtractPointFunction2DAttributes *a = ((ExtractPointFunction2DAttributesObject *)self)->data;
    ExtractPointFunction2DAttributes *b = ((ExtractPointFunction2DAttributesObject *)other)->data;

    switch (op)
    {
       case Py_EQ:
           res = (*a == *b) ? Py_True : Py_False;
           break;
       case Py_NE:
           res = (*a != *b) ? Py_True : Py_False;
           break;
       default:
           res = Py_NotImplemented;
           break;
    }

    Py_INCREF(res);
    return res;
}

//
// Helper functions for object allocation.
//

static ExtractPointFunction2DAttributes *defaultAtts = 0;
static ExtractPointFunction2DAttributes *currentAtts = 0;

static PyObject *
NewExtractPointFunction2DAttributes(int useCurrent)
{
    ExtractPointFunction2DAttributesObject *newObject;
    newObject = PyObject_NEW(ExtractPointFunction2DAttributesObject, &ExtractPointFunction2DAttributesType);
    if(newObject == NULL)
        return NULL;
    if(useCurrent && currentAtts != 0)
        newObject->data = new ExtractPointFunction2DAttributes(*currentAtts);
    else if(defaultAtts != 0)
        newObject->data = new ExtractPointFunction2DAttributes(*defaultAtts);
    else
        newObject->data = new ExtractPointFunction2DAttributes;
    newObject->owns = true;
    newObject->parent = 0;
    return (PyObject *)newObject;
}

static PyObject *
WrapExtractPointFunction2DAttributes(const ExtractPointFunction2DAttributes *attr)
{
    ExtractPointFunction2DAttributesObject *newObject;
    newObject = PyObject_NEW(ExtractPointFunction2DAttributesObject, &ExtractPointFunction2DAttributesType);
    if(newObject == NULL)
        return NULL;
    newObject->data = (ExtractPointFunction2DAttributes *)attr;
    newObject->owns = false;
    newObject->parent = 0;
    return (PyObject *)newObject;
}

///////////////////////////////////////////////////////////////////////////////
//
// Interface that is exposed to the VisIt module.
//
///////////////////////////////////////////////////////////////////////////////

PyObject *
ExtractPointFunction2DAttributes_new(PyObject *self, PyObject *args)
{
    int useCurrent = 0;
    if (!PyArg_ParseTuple(args, "i", &useCurrent))
    {
        if (!PyArg_ParseTuple(args, ""))
            return NULL;
        else
            PyErr_Clear();
    }

    return (PyObject *)NewExtractPointFunction2DAttributes(useCurrent);
}

//
// Plugin method table. These methods are added to the visitmodule's methods.
//
static PyMethodDef ExtractPointFunction2DAttributesMethods[] = {
    {"ExtractPointFunction2DAttributes", ExtractPointFunction2DAttributes_new, METH_VARARGS},
    {NULL,      NULL}        /* Sentinel */
};

static Observer *ExtractPointFunction2DAttributesObserver = 0;

std::string
PyExtractPointFunction2DAttributes_GetLogString()
{
    std::string s("ExtractPointFunction2DAtts = ExtractPointFunction2DAttributes()\n");
    if(currentAtts != 0)
        s += PyExtractPointFunction2DAttributes_ToString(currentAtts, "ExtractPointFunction2DAtts.", true);
    return s;
}

static void
PyExtractPointFunction2DAttributes_CallLogRoutine(Subject *subj, void *data)
{
    typedef void (*logCallback)(const std::string &);
    logCallback cb = (logCallback)data;

    if(cb != 0)
    {
        std::string s("ExtractPointFunction2DAtts = ExtractPointFunction2DAttributes()\n");
        s += PyExtractPointFunction2DAttributes_ToString(currentAtts, "ExtractPointFunction2DAtts.", true);
        cb(s);
    }
}

void
PyExtractPointFunction2DAttributes_StartUp(ExtractPointFunction2DAttributes *subj, void *data)
{
    if(subj == 0)
        return;

    currentAtts = subj;
    PyExtractPointFunction2DAttributes_SetDefaults(subj);

    //
    // Create the observer that will be notified when the attributes change.
    //
    if(ExtractPointFunction2DAttributesObserver == 0)
    {
        ExtractPointFunction2DAttributesObserver = new ObserverToCallback(subj,
            PyExtractPointFunction2DAttributes_CallLogRoutine, (void *)data);
    }

}

void
PyExtractPointFunction2DAttributes_CloseDown()
{
    delete defaultAtts;
    defaultAtts = 0;
    delete ExtractPointFunction2DAttributesObserver;
    ExtractPointFunction2DAttributesObserver = 0;
}

PyMethodDef *
PyExtractPointFunction2DAttributes_GetMethodTable(int *nMethods)
{
    *nMethods = 1;
    return ExtractPointFunction2DAttributesMethods;
}

bool
PyExtractPointFunction2DAttributes_Check(PyObject *obj)
{
    return (obj->ob_type == &ExtractPointFunction2DAttributesType);
}

ExtractPointFunction2DAttributes *
PyExtractPointFunction2DAttributes_FromPyObject(PyObject *obj)
{
    ExtractPointFunction2DAttributesObject *obj2 = (ExtractPointFunction2DAttributesObject *)obj;
    return obj2->data;
}

PyObject *
PyExtractPointFunction2DAttributes_New()
{
    return NewExtractPointFunction2DAttributes(0);
}

PyObject *
PyExtractPointFunction2DAttributes_Wrap(const ExtractPointFunction2DAttributes *attr)
{
    return WrapExtractPointFunction2DAttributes(attr);
}

void
PyExtractPointFunction2DAttributes_SetParent(PyObject *obj, PyObject *parent)
{
    ExtractPointFunction2DAttributesObject *obj2 = (ExtractPointFunction2DAttributesObject *)obj;
    obj2->parent = parent;
}

void
PyExtractPointFunction2DAttributes_SetDefaults(const ExtractPointFunction2DAttributes *atts)
{
    if(defaultAtts)
        delete defaultAtts;

    defaultAtts = new ExtractPointFunction2DAttributes(*atts);
}

